<template>
  <div class="table">
    <div class="container">
      <div class="handle-box">
        <el-input v-model="query.name" placeholder="名称" class="handle-input mr10"></el-input>
        <el-select placeholder="所属项目" v-model="query.projectId" class="handle-input mr10">
          <el-option v-for="option in option.project" :key='option.id' :value="option.id" :label="option.name" />
        </el-select>
        <el-button type="primary" icon="search" @click="handleSearch">
          <i class="el-icon-search"> 搜索</i>
        </el-button>
        <el-button type="primary" @click="handleRefresh">
          <i class="el-icon-refresh"> 重置</i>
        </el-button>
        <el-button style="float: right;" icon="el-icon-lx-add" type="button" v-has="'module_add'" @click="() => handleAdd()">新增</el-button>
      </div>
      <el-table :data="list.data" border v-loading="list.loading" class="table" ref="multipleTable">
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="name" label="名称" v-if="this.$permissions.checkColumn('module_Name')">
        </el-table-column>
        <el-table-column prop="description" label="描述" v-if="this.$permissions.checkColumn('module_Desc')">
        </el-table-column>
        <el-table-column prop="sort" label="排序" v-if="this.$permissions.checkColumn('module_Sort')">
        </el-table-column>
        <el-table-column prop="status" label="状态" v-if="this.$permissions.checkColumn('module_Status')">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status==1" type="success">启用</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240" align="center" v-if="this.$permissions.checkColumn('module_opera')">
          <template slot-scope="scope">
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)" v-has="'module_edit'">编辑</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.row)" v-has="'module_del'">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background @size-change="handlePageSize" @current-change="handlePageIndex" :current-page="query.pageIndex" :page-sizes="[10, 20, 30, 50, 150, 200]" layout="total, sizes, prev, pager, next, jumper" :page-size="query.pageSize" :total="list.total">
        </el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog :title="form.type" :visible.sync="form.show" width="30%">
      <el-form ref="form" :model="form.model" :rules="form.rules" label-width="100px">
        <el-form-item label="名称" prop="name">
          <el-input v-model="form.model.name"></el-input>
        </el-form-item>
        <el-form-item label="所属项目" prop="projectId">
          <el-select placeholder="所属项目" v-model="form.model.projectId" class="handle-input mr10">
            <el-option v-for="option in option.project" :key='option.id' :value="option.id" :label="option.name" />
          </el-select>
        </el-form-item>
        <el-form-item label="描述"  prop="description">
          <el-input v-model="form.model.description"></el-input>
        </el-form-item>
        <el-form-item label="排序"  prop="sort">
          <el-input v-model="form.model.sort"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="form.model.status">
            <el-radio :label="1">启用</el-radio>
            <el-radio :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="form.show = false">取 消</el-button>
        <el-button type="primary" @click="handleSaveEdit">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
    import { getPage,save,remove } from '@/api/interface/module'
    import { projectOption } from '@/api/option'
    import { isSelected } from '@/utils/validator'
    export default {
        data() {
            return {
                query: {
                    name: '',
                    projectId: 0,
                    pageIndex: 1,
                    pageSize: 10
                },
                list: {
                    loading: false,
                    total: 0,
                    data: []
                },
                option:{
                  project:[]
                },
                form:{
                  show:false,
                  type:"新增",
                  model:{
                    id:0,
                    name:"",
                    projectId: 0,
                    description:"",
                    sort:0,
                    status:1,
                  },
                  rules: {
                      name: [
                          { required: true, message: '请输入名称', trigger: 'blur' }
                      ],
                      projectId: [
                          { validator: isSelected, trigger: 'blur' }
                      ]
                  }
                }
            }
        },
        created() {
            this.getListData();  
            this.getProjectOption();          
        },
        computed: {
            data() {
            }
        },
        methods: {
            getListData() {
              this.list.loading = true;
              getPage(this.query).then(res => {
                this.list.data = res.data.items;
                this.list.total = res.data.totalCount;
                this.list.loading = false;
              })
            },
            getProjectOption(){
              projectOption().then(res => {
                this.option.project = res.data
              })
            },
            handlePageSize(pageSize) {
                this.query.pageSize = pageSize
                this.getListData()
            },
            handlePageIndex(pageIndex) {
                this.query.pageIndex = pageIndex
                this.getListData()
            },
            handleSearch() {
                this.query.pageIndex = 1
                this.query.pageSize = 10
                this.getListData()
            },
            handleRefresh() {
              this.query = {
                name: '',
                projectId: 0,
                pageIndex: 1,
                pageSize: 10
              }
              this.getListData()
            },
            handleAdd(){
              this.form.model = {
                    id:0,
                    name:"",
                    projectId: 0,
                    description:"",
                    sort:0,
                    status:1,
              }
              this.form.type = "新增";
              this.form.show = true;
            },
            handleEdit(row){
              this.form.model = Object.assign({}, row);
              this.form.type = "编辑";
              this.form.show = true;
            },
            handleSaveEdit(){
              console.log(1)
              this.$refs["form"].validate((valid) => {
                if (valid) {
                  save(this.form.model).then(res => {
                    if (res.status === 200) {
                      this.form.show = false;
                      this.$message({
                        message: "操作成功",
                        type: "success"
                      });
                      this.getListData();
                    }
                  })
                  }
                  else{
                    this.$message({ message: "输入错误", type: 'error' })
                    return false;
                  }
                })
            },
            handleDelete(data) {
              this.$confirm("此操作将永久删除, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
              })
                .then(() => {
                  remove({ Id: data.id }).then(res => {
                    if (res.status === 200) {
                      this.form.show = false;
                      this.$message({
                        message: "删除成功",
                        type: "success"
                      });
                      this.getListData();
                    }
                  });
                })
                .catch(() => {
                  this.$message({
                    type: "info",
                    message: "已取消删除"
                  });
                });
            },
        }
    }

</script>

<style scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
</style>
